//
//  BRSearchView.m
//  Kexu
//
//  Created by IOS on 2023/7/14.
//

#import "BRSearchView.h"

@interface BRSearchView ()<QMUITextFieldDelegate>

@end

@implementation BRSearchView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - textField  Delegate --
- (void)textFieldDidChangeSelection:(UITextField *)textField {
    
//    if (textField.text.length == 0) {
//        if (self.researchBlock) {
//            self.researchBlock();
//        }
//    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldShouldBeginEditing = %@",textField.text);
    
//    if (textField == self.textTF) {
//        if (textField.text.length >= 25) {
//            return NO;
//        }
//    }
//
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"searchView-[%@][%@]",textField.text,string);
    if ([string isEqualToString:@"\n"]) {
        [textField endEditing:YES];
        if (self.searchStrBlock) {
//            NSString *search = [NSString stringWithFormat:@"%@%@",textField.text,string];
            self.searchStrBlock(textField.text);
        }
        
        return NO;
    }
    if (range.length == 1 && string.length == 0 && textField.text.length == 1) {
        // 删除最后一个
        if (self.researchBlock) {
            self.researchBlock();
        }
    }
   
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if (self.researchBlock) {
        self.researchBlock();
    }
    return YES;
}

- (void)setText:(NSString *)text {
    self.textField.text = text;
}

- (NSString *)text {
    return self.textField.text;
}

- (void)setPlaceholder:(NSString *)placeholder {
   self.textField.placeholder = placeholder;
}


#pragma mark - UI --
- (void)setUI {
    
    self.backgroundColor = kColorBGF5;
    self.layer.cornerRadius = 16;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.leftImg];
    [self.leftImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.centerY.offset(0);
        make.width.height.mas_equalTo(16);
    }];
    
    [self addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftImg.mas_right).offset(4);
        make.right.offset(0);
        make.top.offset(4);
        make.bottom.offset(-4);
    }];
}

- (UIImageView *)leftImg {
    
    if (_leftImg == nil) {
        _leftImg = [[UIImageView alloc] initWithImage:UIImageMake(@"search_gray12")];
    }
    return _leftImg;
}

- (QMUITextField *)textField {
    
    if (_textField == nil) {
        _textField = [[QMUITextField alloc] init];
        _textField.placeholder = @"";
        _textField.font = [UIFont br_fontM:12];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.minimumFontSize = 10;
        _textField.adjustsFontSizeToFitWidth = YES;
        _textField.clearButtonPositionAdjustment = UIOffsetMake(-12, 0);
        _textField.placeholderColor = kColorTitle210;
        _textField.textColor =  kColorTitle;
        _textField.delegate = self;
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.tintColor = kColorMain;
    }
    return _textField;
}


@end
